//图片
$(".btn-picture").parent("li:first").click(function() {
        currentClickTextId=-1;//标记当前选中的不是textarea;
        currentClickRadioId=-1;
        currentClickVideoId = -1;
        
        // 循环文本ID
        imageId++;
        currentClickImageId = imageId;
        var imagestr = "<div id='image-"
                        + imageId
                        + "' style='position: absolute;left:"+pptLeft+"px;top:"+pptTop+"px;'>"
                        + "<div id='image-title-"
                        + imageId
                        + "' class='image-tit'>" 
                                + "<span><img src='../images/ppt/icon01.png' class='v-middle'/>&nbsp;<strong>点击这里拖动</strong>&nbsp;</span>"
                                + "<img src='../images/ppt/error_tips.png' class='image-delete v-middle' onclick='divDelete(this);'/>"
                        +"</div>"
                        + "<img id='image-content-"+imageId+"' class='imageContent' style='width:100px;height:100px;' src='../images/default.png' />" 
                        + "</div>";
        $(".c-content").append(imagestr);
        $(".picture-layout").fadeIn("slow");
        //图像信息
        imageListener(imageId);
        updateImageLayout(imageId);
        
        //更新
        ppt.reSave();
});

$(".picture-layout .pic-tab li").click(function(){
        $(this).siblings("li").removeClass("cover").end().addClass("cover");
        var name=$(this).attr("name");
        $("."+name).siblings("div").addClass("hide").end().removeClass("hide");
        $(".special-div").show();
});


/**
 * image事件
 * @param imageid int
 */
function imageListener(imageid){
        // 文本输入框和配置数据都赋予拖拽功能
        $("#image-"+imageid ).draggable({
                //containment: ".c-content"
        });
        
        
        //属性框
        $(".picture-layout").draggable();
        
        /**
         * 
         */
        $("#image-"+imageid).click().mousedown(function(e){
        		isReadyToStretch(e,imageid);
        	
                isMouseDown=true;
                currentClickImageId=imageid;
                
                currentClickTextId=-1;
                currentClickRadioId=-1;
                currentClickVideoId = -1;
                
        		if(isToStretch){
        			$("#image-"+imageid ).removeData("draggable").unbind(".draggable").removeClass("ui-draggable" + " ui-draggable-dragging"+ " ui-draggable-disabled");
        		}
        		else{
                    $("#image-"+imageid).fadeTo(10,0.5);
                    $("#image-"+imageid).css("z-index", "" + (++zindex));
                    updateImageLayout(imageid);
        		}
                
        });
        
        //拖动
        $("#image-"+imageid).click().mousemove(function(e){
                if(isMouseDown){
                	if(!isToStretch){
                		 updateImageLayout(imageid);
                	}
                	//图片放大与缩小
                	else{
                		$("#image-"+imageid ).removeClass("ui-draggable-dragging");
                		//让图片左上角保持不变
                        $("#image-" + imageid).css("left", stretchX);
                        $("#image-" + imageid).css("top", stretchY);
                                              
                        //得到当前鼠标坐标,偏移
                        var mouse = getMousePosition(e);
                        var mouse_move_x = mouse.x-stretchX;
                        var mouse_move_y = mouse.y-stretchY;
                        
                        $("#image-content-"+currentClickImageId).css("width",mouse_move_x+stretchMX);
                        $("#image-content-"+currentClickImageId).css("height",mouse_move_y+stretchMY);
                        updateImageLayout(imageid);
                	}
                }
        });
        
        
        $("#image-"+imageid).click().mouseup(function(){
                isMouseDown=false;
                isToStretch=false;
                $("#image-"+imageid ).draggable({});
               
                currentClickImageId=imageid;
                
                currentClickTextId=-1;
                currentClickRadioId=-1;
                currentClickVideoId = -1;
                
                $("#image-"+imageid).fadeTo(10,1);
                updateImageLayout(imageid);
        });
        
        $("#image-"+imageid).click().mouseout(function(){
                $("#image-"+imageid).fadeTo(10,1);
                if(!isToStretch){
                    $("#image-"+imageid ).draggable({
                    });
                }
        });
        
        
}

/**
 * //网络图片
 */
$("#image-submit").click(function(){
        if(currentClickImageId==-1){
                alert("请选 中一个图像框体");
                return;
        }
        
        var src,x,y,w,h;
        
        if(!$("#picture1").is("hidden")){
                src = $("#netimage").val();
                if(src!=""){
                        $("#image-content-"+currentClickImageId).attr("src",src);
                }
                
                //x y 
                x = $("#image-x-coordinate").val();
                y = $("#image-y-coordinate").val();
                var newpos = adjustPosition(".c-content", "#image-" + currentClickImageId, "#image-content-" + currentClickImageId);
                $("#image-" + currentClickImageId).css("left", parseInt(x)+newpos.pleft);
                $("#image-" + currentClickImageId).css("top", parseInt(y)+newpos.ptop);
                
                newpos = adjustPosition(".c-content", "#image-" + currentClickImageId, "#image-content-" + currentClickImageId);
                x = newpos.left;
                y = newpos.top;
                // 更改位置
                $("#image-" + currentClickImageId).css("left", x);
                $("#image-" + currentClickImageId).css("top", y);
                $("#image-x-coordinate").val(parseInt(x)-newpos.pleft);
                $("#image-y-coordinate").val(parseInt(y)-newpos.ptop);
                
                //w h 
                w = $("#image-width").val();
                h = $("#image-height").val();
                $("#image-content-"+currentClickImageId).css("width",w);
                $("#image-content-"+currentClickImageId).css("height",h);
                
                //增加一个属性，显示时间
                var imageshowtime=parseInt($("#image-showtime").val().trim());
                if(isNaN(imageshowtime)){
                        alert("时间：请输入数字");
                        return;
                }
                else if(imageshowtime<0){
                        alert("时间：输入数字必须大于等于0");
                        return;
                }
                $("#image-"+currentClickImageId).attr("showtime",""+imageshowtime);
        }
});

/**
 * 图片上传后确认更改
 */
$("#image-upload-submit").click(function(){
        if(currentClickImageId==-1){
                alert("请选 中一个图像框体");
                return;
        }
        
        var src = $("#imageUploadSrc").val();
        if(src!=""){
                $("#image-content-"+currentClickImageId).attr("src",src);
        }
});


/**
 * 更新图片属性框信息
 */
function updateImageLayout(imageid){
        var newpos = adjustPosition(".c-content", "#image-" + imageid, "#image-content-" + imageid);
        
        var x = (isNaN(newpos.left) ? newpos.pleft : newpos.left);
        var y = (isNaN(newpos.top) ? newpos.ptop : newpos.top);;

        $("#image-x-coordinate").val(parseInt(x - newpos.pleft));
        $("#image-y-coordinate").val(parseInt(y - newpos.ptop));

        // 更改位置 x y
        $("#image-" + imageid).css("left", x);
        $("#image-" + imageid).css("top", y);
        
        //w h
        var w = $("#image-content-" + imageid).width();
        var h = $("#image-content-" + imageid).height();
        $("#image-width").val(w);
        $("#image-height").val(h);
        
        //src
        var src = $("#image-content-"+imageid).attr("src");
        $("#netimage").val(src);
        $("#imageUploadSrc").val(src);
        
        var fullscreen =  $("#image-content-"+imageid).attr("fullscreen");
        if(fullscreen=="true"){
        	$("#image-fullscreen").attr("checked",true);
        }
        else{
        	$("#image-fullscreen").attr("checked",false);
        }
        
}


/**
 * 图片上传时验证
 * @returns {Boolean}
 */
function checkImage() {
        var imagefile = $("#uploadFile").val();

        if (imagefile == undefined || imagefile == "" || imagefile == null) {
                alert("请选择要上传的执照图片");
                return false;
        }

        var index = imagefile.lastIndexOf("\\");
        var name = imagefile.substr(index + 1, imagefile.length);
        index = name.lastIndexOf(".");
        var suffix = name.substr(index + 1, name.length).toLowerCase();

        if (suffix == "jpg" || suffix == "jpeg" || suffix == "png"
                        || suffix == "bmp") {
                return true;
        }

        alert("只能上传jpg、jpeg、png、bmp格式的图片");
        return false;
}

/**
 * 判断是否点击在该图片的右下角的某个区域
 * @param e
 * @param imageid 当前图片的ID
 */
function isReadyToStretch(e,imageid){
	isToStretch=false;
	//得到鼠标的坐标
	var mouse = getMousePosition(e);
	//console.info(mouse);
	
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#image-" + imageid, "#image-content-" + imageid);
    var x = (isNaN(newpos.left) ? newpos.pleft : newpos.left);
    var y = (isNaN(newpos.top) ? newpos.ptop : newpos.top);
    var w = newpos.w;
    var h = newpos.h;
    //console.info("asdfdsf"+(x+0.9*w)+" "+(y+0.9*h)+" "+(x+w)+" "+(y+h));
    
    //20是偏差
    if((x+0.8*w<mouse.x && y+0.8*h<mouse.y-20)&&(mouse.x<x+w && mouse.y-20<y+h)){
    	isToStretch=true;
    	//图片左上角坐标
    	stretchX=x;
    	stretchY=y;
    	//鼠标距离图片右下角的偏移
    	stretchMX=x+w-(mouse.x-20);
    	stretchMY=y+h-(mouse.y-20);
    }
    else{
    	isToStretch=false;
    }
    
}

/**
 * 上下居中
 */
$("#image-up-down-center").click(function(){
    if(currentClickImageId==-1){
        alert("请选 中一个图像框体");
        return;
    }
	
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#image-" + currentClickImageId, "#image-content-" + currentClickImageId);
   // var x = (isNaN(newpos.left) ? newpos.pleft : newpos.left);
   // var y = (isNaN(newpos.top) ? newpos.ptop : newpos.top);
    //var w = newpos.w;
    var h = newpos.h;
    
    //居中时左上角坐标，只要考虑top
    var newtop = parseInt((newpos.ptop+newpos.pendtop)/2-h/2)-9;
    $("#image-" + currentClickImageId).css("top", newtop);
    $("#image-y-coordinate").val(parseInt(newtop - newpos.ptop));
    
});


/**
 * 左右居中
 */
$("#image-left-right-center").click(function(){
    if(currentClickImageId==-1){
        alert("请选 中一个图像框体");
        return;
    }
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#image-" + currentClickImageId, "#image-content-" + currentClickImageId);
   // var x = (isNaN(newpos.left) ? newpos.pleft : newpos.left);
   // var y = (isNaN(newpos.top) ? newpos.ptop : newpos.top);
    var w = newpos.w;
   // var h = newpos.h;
    
    //居中时左上角坐标，只要考虑left
    var newleft = parseInt((newpos.pleft+newpos.pendleft)/2-w/2);
    $("#image-" + currentClickImageId).css("left", newleft);
    $("#image-x-coordinate").val(parseInt(newleft - newpos.pleft));
});


/**
 * 点击图片充满整个PPT容器
*/

$("#image-fullscreen").click(function(){
        var check = $(this).attr("checked");
        
        var newpos = adjustPosition(".c-content", "#image-" + currentClickImageId, "#image-content-" + currentClickImageId);
        if(check=="checked" || check==true){
        	$("#image-content-"+currentClickImageId).attr("fullscreen","true");
        	//保存老的信息,以便恢复时还原
        	var old_w = newpos.w;
        	var old_h = newpos.h;
        	var old_left = newpos.left;
        	var old_top =  newpos.top;
        	
        	$("#image-content-"+currentClickImageId).attr("old_w",old_w);
        	$("#image-content-"+currentClickImageId).attr("old_h",old_h);
        	$("#image-content-"+currentClickImageId).attr("old_left",old_left);
        	$("#image-content-"+currentClickImageId).attr("old_top",old_top);
        	
        	//全屏
        	var new_w = parseInt(newpos.pendleft-newpos.pleft);
        	var new_h = parseInt(newpos.pendtop-newpos.ptop);
        	
            $("#image-" + currentClickImageId).css("left", 0+newpos.pleft);
            $("#image-" + currentClickImageId).css("top", 0+newpos.ptop);
            $("#image-content-" + currentClickImageId).width(new_w);
            $("#image-content-" + currentClickImageId).height(new_h);
            
            //更新属性框中的信息
            $("#image-x-coordinate").val(0);
            $("#image-y-coordinate").val(0);
            $("#image-width").val(new_w);
            $("#image-height").val(new_h);
        }
        else{
        	$("#image-content-"+currentClickImageId).attr("fullscreen","false");
        	var old_w = $("#image-content-"+currentClickImageId).attr("old_w");
        	var old_h = $("#image-content-"+currentClickImageId).attr("old_h");
        	var old_left = $("#image-content-"+currentClickImageId).attr("old_left");
        	var old_top =  $("#image-content-"+currentClickImageId).attr("old_top");
        	
            //更新属性框中的信息
            $("#image-x-coordinate").val(old_left-newpos.pleft);
            $("#image-y-coordinate").val(old_top-newpos.ptop);
            $("#image-width").val(old_w);
            $("#image-height").val(old_h);
            
            //要parseInt,不然无效
            $("#image-" + currentClickImageId).css("left", parseInt(old_left));
            $("#image-" + currentClickImageId).css("top",  parseInt(old_top));
            $("#image-content-" + currentClickImageId).width(old_w);
            $("#image-content-" + currentClickImageId).height(old_h);
        }
});
